//
//  JSONTool.swift
//  JianxiaozhiAI
//
//  Created by Jie on 2021/6/2.
//  Copyright © 2021 Jie. All rights reserved.
//

import UIKit

// https://www.advancedswift.com/swift-json-without-swiftyjson/

class JSONTool: NSObject {
    
    // data --> dictionary
    static func dataToString(data: Data?) -> String? {
        var respString: String?
        if let data = data {
            respString = String(data: data, encoding: .utf8)
        }
        return respString
    }
    
    // string --> dictionary
    static func stringToDict(string: Any?) -> [String: Any]? {
        var respDict: [String: Any]?
        if let bodyDict = string as? [String: Any] {
            respDict = bodyDict
        }else if let bodyString = string as? String {
            if let data =  bodyString.data(using: .utf8),
               let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                respDict = dict
            }
        }
        return respDict
    }
    
    
    // dict --> string
    static func dictToString(_ dict: [String : Any]) -> String? {
             let data = try? JSONSerialization.data(withJSONObject: dict, options: [])
             let string = String(data: data!, encoding: String.Encoding.utf8)
             return string
         }
    
    ///jsonArray --> Codable
   static func jsonArrayToModelArray<T: Decodable>(_ type: T.Type, from resp: Any?) -> [T]? {
       var data: Data?
       if let dict = resp as? [[String: Any]] {
           data = try? JSONSerialization.data(withJSONObject: dict, options: [])
       } else if let string = resp as? String {
           data =  string.data(using: .utf8)
       }
       if let data = data {
           return try? JSONDecoder().decode([T].self, from: data)
       }
       return nil
   }
   
   // string --> Codable
    static func jsonToModel<T: Decodable>(_ type: T.Type, from resp: Any?) -> T? {
       var data: Data?
       if let dict = resp as? [String: Any] {
           data = try? JSONSerialization.data(withJSONObject: dict, options: [])
       }else if let string = resp as? String {
           data =  string.data(using: .utf8)
       }
       if let data = data {
           return try? JSONDecoder().decode(T.self, from: data)
       }
       return nil
   }
    
    // model --> dictionary
    static func modelToDict<T: Encodable>(_ model: T) -> [String: Any]? {
        if let data = try? JSONEncoder().encode(model) {
            let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            return json as? [String: Any]
        }
        return nil
    }
    
    //  model --> string
    static func modelToString<T: Encodable>(_ model: T) -> String? {
        if let data = try? JSONEncoder().encode(model) {
            return String(data: data, encoding: .utf8)
        }
        return nil
    }
 
    //  model --> string
    static func modelArrayToString<T: Encodable>(_ modelArray: [T]) -> String? {
        if let data = try? JSONEncoder().encode(modelArray) {
            return String(data: data, encoding: .utf8)
        }
        return nil
    }
    
    // [model] --> dictionary
    static func modelArrayToDict<T: Encodable>(_ modelArray: [T]) -> [[String: Any]]? {
        if let data = try? JSONEncoder().encode(modelArray) {
            let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            return json as? [[String: Any]]
        }
        return nil
    }
    
}
